<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
         <!--  <el-form-item label="类别" prop="category">
            <el-select v-model="queryParams.category" placeholder="请选择类别" clearable>
              <el-option v-for="dict in fixed_category" :key="dict.id" :label="dict.category" :value="dict.id" />
            </el-select>
          </el-form-item> -->
          <el-form-item label="名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入名称" clearable style="width: 240px" @keyup.enter="handleQuery" @clear="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['business:assetLedger:export']">导出表格</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="assetLedgerList" @selection-change="handleSelectionChange" @sort-change="sortChange">
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="类型" align="center" prop="type">
          <template #default="scope">
            <dict-tag :options="asset_type" :value="scope.row.type" />
          </template>
        </el-table-column> -->
        <!-- <el-table-column label="类别" align="center" prop="category">
          <template #default="scope">
            <span>{{getCategory(scope.row.category)}}</span>
          </template>
        </el-table-column> -->
        <el-table-column label="名称" align="center" prop="name" sortable />
        <el-table-column label="型号" align="center" prop="model" />
        <el-table-column label="单位" align="center" prop="unit" />
        <el-table-column label="数量" align="center" prop="quantity" sortable />
        <el-table-column label="单价" align="center" prop="univalence" sortable />
        <el-table-column label="合计" align="center" prop="total" sortable />
        <el-table-column label="备注" align="center" prop="remark" />
      </el-table>

      <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="AssetLedger" lang="ts">
import { listAssetLedger } from '@/api/business/assetLedger';
import { AssetLedgerVO, AssetLedgerQuery, AssetLedgerForm } from '@/api/business/assetLedger/types';
import { listAssetCategory } from '@/api/business/assetCategory/index';
const fixed_category = ref([])
const xzqueryParams = ref<ProjectQuery>({
  type:undefined
})
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { asset_type } = toRefs<any>(proxy?.useDict('asset_type'));

const assetLedgerList = ref<AssetLedgerVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const initFormData: AssetLedgerForm = {
  id: undefined,
  type: undefined,
  // category: undefined,
  name: undefined,
  model: undefined,
  unit: undefined,
  quantity: undefined,
  univalence: undefined,
  total: undefined,
  remark: undefined,
}
const data = reactive<PageData<AssetLedgerForm, AssetLedgerQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    type: undefined,
    // category: undefined,
    name: undefined,
    model: undefined,
    unit: undefined,
    quantity: undefined,
    univalence: undefined,
    total: undefined,
    isAsc:'asc',
    orderByColumn:'name',
    params: {
    }
  },
  rules: {
    id: [
      { required: true, message: "资产id不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "类型，0固定，1非固定不能为空", trigger: "change" }
    ],
    category: [
      { required: true, message: "类别，1办公，2劳保不能为空", trigger: "change" }
    ],
    name: [
      { required: true, message: "资产名称不能为空", trigger: "blur" }
    ],
    model: [
      { required: true, message: "型号不能为空", trigger: "blur" }
    ],
    unit: [
      { required: true, message: "单位不能为空", trigger: "blur" }
    ],
    quantity: [
      { required: true, message: "数量不能为空", trigger: "blur" }
    ],
    univalence: [
      { required: true, message: "单价不能为空", trigger: "blur" }
    ],
    total: [
      { required: true, message: "合计不能为空", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询资产台账列表 */
const getList = async () => {
  loading.value = true;
  const res = await listAssetLedger(queryParams.value);
  assetLedgerList.value = res.rows.filter( item => {
    if(item.quantity != 0)return item
  })
  total.value = res.total;
  loading.value = false;
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: AssetLedgerVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('business/assetLedger/export', {
    ...queryParams.value
  }, `固定资产台账${new Date().getTime()}.xlsx`)
}

const getCategory = (data) =>{
  if (data && fixed_category.value) {
    let obj = fixed_category.value.find(item=>item.id ==data)
    return obj?.category
  }
}

const sortChange = (column) =>{
  queryParams.value.orderByColumn = column.prop
  if (column.order && queryParams.value.orderByColumn) {
    if (column.order == 'ascending') {//当用户点击的是升序按钮，即ascending时
      queryParams.value.isAsc = 'asc'; //将order这个变量赋值为后端接口文档定义的升序的字段名，之后作为入参传给后端
    } else if (column.order == 'descending') {
    //当用户点击的是升序按钮，即descending时
      queryParams.value.isAsc = 'desc';//将order这个变量赋值为后端接口文档定义的降序的字段名，之后作为入参传给后端
    }
  }else{
    queryParams.value.isAsc = 'asc';
    queryParams.value.orderByColumn = 'name'
  }
  getList()
}

onMounted(() => {
  getList();
  listAssetCategory(xzqueryParams.value).then(res=> {
    if(res.code == 200) {
      fixed_category.value = res.rows
    }
  })
});
</script>
